<template>
  <view class="discussion-container">
    <!-- 游戏banner和名称 -->
    <view class="game-info">
      <image class="game-banner" :src="gameBanner" mode="aspectFill"></image>
      <text class="game-title">{{ gameTitle }}</text>
    </view>
    
    <!-- 讨论列表 -->
    <view class="discussion-list">
      <view class="discussion-item" v-for="(item, index) in discussionList" :key="index">
        <!-- 用户信息 -->
        <view class="user-info">
          <image class="user-avatar" :src="item.userAvatar"></image>
          <view class="user-details">
            <text class="user-name">{{ item.userName }}</text>
            <text class="post-time">{{ item.postTime }}</text>
          </view>
        </view>
        
        <!-- 讨论内容 -->
        <view class="discussion-content">
          <text class="discussion-text">{{ item.content }}</text>
          <view class="discussion-images" v-if="item.images && item.images.length">
            <image v-for="(img, imgIndex) in item.images" 
                   :key="imgIndex" 
                   :src="img" 
                   class="content-image"
                   mode="aspectFill">
            </image>
          </view>
        </view>
        
        <!-- 互动栏 -->
        <view class="interaction-bar">
          <view class="interaction-item" @click="handleLike(index)" :class="{ 'liked': item.isLiked }">
            <text class="interaction-icon">{{ item.isLiked ? '❤️' : '🤍' }}</text>
            <text class="interaction-count">{{ item.likes }}</text>
          </view>
          <view class="interaction-item">
            <text class="interaction-icon">💬</text>
            <text class="interaction-count">{{ item.comments }}</text>
          </view>
        </view>
      </view>
    </view>
    
    <!-- 发表评论区域 -->
    <view class="comment-section">
      <view class="comment-input-area">
        <input 
          class="comment-input" 
          v-model="newComment.content"
          placeholder="说点什么..."
          :maxlength="200"
        />
        <view class="comment-toolbar">
          <view class="tool-item" @click="chooseImage">
            <text class="tool-icon">📷</text>
          </view>
          <button class="submit-button" @click="submitComment">发送</button>
        </view>
      </view>
    </view>
  </view>
</template>
<script>
import { games } from '../../utils/gameData.js';
export default {
  data() {
    return {
      gameId: '',
      gameTitle: '',
      gameBanner: '',
      newComment: {
        content: '',
        images: []
      },
      discussionList: [
        {
          userName: "游戏玩家",
          userAvatar: "/static/avatar/default.png",
          postTime: "2小时前",
          content: "这个游戏真的很好玩！",
          images: [],
          likes: 128,
          comments: 32,
          isLiked: false
        }
      ]
    }
  },
  onLoad(option) {
    // 获取传递的游戏ID
    this.gameId = option.gameId;
    // 根据ID获取游戏信息
    const game = games.find(g => g.game_id === this.gameId);
    if (game) {
      this.gameTitle = game.game_name;
      this.gameBanner = game.game_banner;
    }
  },
  methods: {
    chooseImage() {
      uni.chooseImage({
        count: 9 - (this.newComment.images?.length || 0),
        success: (res) => {
          this.newComment.images = [...(this.newComment.images || []), ...res.tempFilePaths];
        }
      });
    },
    removeImage(index) {
      this.newComment.images.splice(index, 1);
    },
    handleLike(index) {
      const post = this.discussionList[index];
      if (post.isLiked) {
        post.likes--;
      } else {
        post.likes++;
      }
      post.isLiked = !post.isLiked;
      
      // 添加点赞动画效果
      uni.vibrateShort(); // 触发短振动反馈
    },
    submitComment() {
      if (!this.newComment.content.trim() && (!this.newComment.images || !this.newComment.images.length)) {
        uni.showToast({
          title: '请输入内容或添加图片',
          icon: 'none'
        });
        return;
      }
      
      // 添加新评论到列表
      this.discussionList.unshift({
        userName: "当前用户",
        userAvatar: "/static/avatar/default.png",
        postTime: "刚刚",
        content: this.newComment.content,
        images: [...this.newComment.images],
        likes: 0,
        comments: 0,
        isLiked: false
      });
      
      // 重置评论框
      this.newComment = {
        content: '',
        images: []
      };
    }
  }
}
</script>
<style>
.discussion-container {
  background: #1E1E2E;
  min-height: 100vh;
  padding-bottom: 60px; /* 为底部评论框留出空间 */
}
.game-info {
  width: 100%;
  position: relative;
}
.game-banner {
  width: 100%;
  height: 200px;
}
.game-title {
  position: absolute;
  bottom: 20px;
  left: 20px;
  color: #fff;
  font-size: 24px;
  font-weight: bold;
  text-shadow: 0 2px 4px rgba(0,0,0,0.5);
}
.discussion-list {
  padding: 15px;
}
.discussion-item {
  background: #2D2D44;
  border-radius: 12px;
  padding: 15px;
  margin-bottom: 15px;
}
.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 20px;
  margin-right: 10px;
}
.user-details {
  flex: 1;
}
.user-name {
  color: #fff;
  font-size: 16px;
  font-weight: bold;
}
.post-time {
  color: #6E6C89;
  font-size: 12px;
}
.discussion-content {
  margin: 10px 0;
}
.discussion-text {
  color: #fff;
  font-size: 14px;
  line-height: 1.5;
}
.discussion-images {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}
.content-image {
  width: 100px;
  height: 100px;
  border-radius: 8px;
}
.interaction-bar {
  display: flex;
  gap: 20px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid #4D4C6D;
}
.interaction-item {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 10px;
  border-radius: 15px;
  transition: all 0.3s ease;
}
.interaction-item:active {
  transform: scale(0.95);
  opacity: 0.8;
}
.liked {
  color: #ff4757;
}
.liked .interaction-count {
  color: #ff4757;
}
.interaction-icon {
  font-size: 18px;
  transition: transform 0.2s ease;
}
.interaction-item:active .interaction-icon {
  transform: scale(1.2);
}
.interaction-count {
  color: #6E6C89;
  font-size: 14px;
}
.comment-section {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #2D2D44;
  padding: 10px;
  border-top: 1px solid #4D4C6D;
}
.comment-input-area {
  display: flex;
  align-items: center;
  background: #1E1E2E;
  border-radius: 20px;
  padding: 5px 10px;
}
.comment-input {
  flex: 1;
  height: 36px;
  color: #fff;
  font-size: 14px;
  background: transparent;
}
.comment-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
}
.tool-item {
  padding: 5px;
}
.submit-button {
  background: #4D4C6D;
  color: #fff;
  font-size: 14px;
  padding: 5px 12px;
  border-radius: 15px;
  border: none;
  height: 30px;
  line-height: 20px;
}
</style> 
